package design_pattern.singleton;

/**
 * @Name : singleton.SingleTon02
 * @Description : 静态代码块，线程不安全，懒加载
 * Created by cyd on 2021-02-02 11:57:29
 */
public class SingleTon02 {

    public static void main(String[] args) {
        // 测试，拿到的实例是同一个对象
        Singlton02 instance = Singlton02.getInstance();
        Singlton02 instance1 = Singlton02.getInstance();
        System.out.println(instance == instance1);
    }
}

class Singlton02 {
    // 1.私有化构造器
    private Singlton02() {
    }

    // 2.本例内部声明对象实例
    private static Singlton02 instance;

    // 3.静态代码块中创建实例对象
    static {
        instance = new Singlton02();
    }

    // 3.提供一个共有的静态方法，返回这个实例对象
    public static Singlton02 getInstance() {
        return instance;
    }
}
